/**
 * All Rights Reserved , Copyright (C) 2022 , 青岛鼎信通讯股份有限公司
 *
 *MpmCustomerTaskController
 * 项目策划-客户任务子表
 *
 * 修改纪录
 * 2022-03-10 版本：1.0 houyuhang 创建。
 * @version 版本：1.0
 * @author 作者：houyuhang
 * 创建日期：2022-03-10
 */

package com.topscomm.mpm.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.convert.Convert;
import com.topscomm.basic.BasicEntity;
import com.topscomm.mpm.pojo.*;
import com.topscomm.mpm.service.impl.MpmCustomerAnalysisService;
import com.topscomm.mpm.service.impl.MpmProjectPlanService;
import com.topscomm.pub.util.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import com.topscomm.mpm.controller.auto.MpmCustomerTaskControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: houyuhang
 * @date: 2022-03-10
 * @description:项目策划-客户任务子表Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/customerTask")
public class MpmCustomerTaskController extends MpmCustomerTaskControllerAuto implements IMpmCustomerTaskController {
	@Autowired
	private MpmCustomerAnalysisService mpmCustomerAnalysisService;
	@Autowired
	private MpmProjectPlanService mpmProjectPlanService;
	/**
	 * @author: houyuhang
	 * @date: 2022-03-10
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: houyuhang
	 * @date: 2022-03-10
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @author: houyuhang
	 * @date: 2022-03-10
	 * @param: paraMap
	 * @description: 加载客户任务相关信息
	 * @return: ResponseResult<Object>
	 * @modify:
	 */
	@GetMapping(value = "/customerTaskLoadInfo")
	public ResponseResult<Object> customerTaskLoadInfo(@RequestParam(required = false) Map<String, Object> paraMap) {
		String planid = ConvertUtil.convertToString(paraMap.get("id"));
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle("planid = '{0}' order by sortcode asc,id asc",planid);
		// 获取客户任务信息
		List<Map<String, Object>> customerTaskList = this.service.queryMapByWhere(sql.toString());
		paraMap.put("customerTaskList", customerTaskList);
		return ResponseResult.ok(paraMap);
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-03-11
	 * @param: dataMap
	 * @param: paraMap
	 * @description: 保存全部客户任务
	 * @return: ResponseResult<Object>
	 * @modify:
	 */
	@Transactional
	@PostMapping(value = "/updateInfo")
	public ResponseResult<Object> updateInfo(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			List<Map<String, Object>> customerTaskList = (List<Map<String, Object>>) paraMap.get("customerTaskList");
			String planid = ConvertUtil.convertToString(paraMap.get("planid"));
			List<BasicEntity> batchInsert = new ArrayList<BasicEntity>();
			List<Map<String, Object>> batchUpdate = new ArrayList<Map<String, Object>>();
			for (Map<String, Object> customerTask : customerTaskList) {
				if (customerTask.get("id") != null) {
					batchUpdate.add(customerTask);
				} else {
					customerTask.put("id", "");
					customerTask.put("planid", planid);
					BasicEntity milepostEntity = new MpmProjectMilepostEntity();
					milepostEntity = milepostEntity.convertFromMap(customerTask);
					batchInsert.add(milepostEntity);
				}
			}
			this.service.insertBatch(batchInsert);
			this.service.updateBatchMap(batchUpdate);
			return ResponseResult.ok("更新成功！");
		} catch (Exception ex) {
			return ResponseResult.error("更新客户任务出错，请联系管理员!");
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-03-11
	 * @param: dataMap
	 * @param: paraMap
	 * @description: 新增客户任务
	 * @return: ResponseResult<Object>
	 * @modify:
	 */
	@Transactional
	@PostMapping(value = "/addCustomerTask")
	public ResponseResult<Object> addCustomerTask(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			long planid = ConvertUtil.convertToLong(paraMap.get("planid"));
			int sortcode = ConvertUtil.convertToInt(paraMap.get("sortcode"));
			MpmCustomerTaskEntity mpmCustomerTaskEntity = new MpmCustomerTaskEntity();
			mpmCustomerTaskEntity.setId(0L);
			mpmCustomerTaskEntity.setPlanid(planid);
			mpmCustomerTaskEntity.setSortCode(sortcode);
			MpmCustomerTaskEntity res = this.service.insert(mpmCustomerTaskEntity);
			return ResponseResult.ok(res);
		} catch (Exception ex) {
			return ResponseResult.error("新增客户任务出错，请联系管理员!");
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-03-11
	 * @param: paraMap
	 * @description: 删除客户任务及客户决策链子表信息
	 * @return: ResponseResult<Object>
	 * @modify:
	 */
	@Transactional
	@DeleteMapping(value = "/removeCustomerTask")
	public ResponseResult<Object> removeCustomerTask(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			String id = ConvertUtil.convertToString(paraMap.get("id"));
			StringBufferProxy sql = new StringBufferProxy();
			sql.appendSingle(" {0} = '{1}' ", MpmCustomerAnalysisEntity.FieldCustomertaskid, id);
			// 删除子表数据
			mpmCustomerAnalysisService.deleteByWhere(sql.toString());
			// 删除主表数据
			this.service.deleteById(id);
			return ResponseResult.ok("删除成功");
		} catch (Exception ex) {
			return ResponseResult.error("删除客户任务出错，请联系管理员!");
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-03-18
	 * @param: paraMap
	 * @description: 系统部监管加载客户任务相关信息
	 * @return: ResponseResult<Object>
	 * @modify:
	 */
	@GetMapping(value = "/queryCustomerTaskForSystem")
	public ResponseResult<Object> queryCustomerTaskForSystem(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			List<Map<String,Object>> res = this.service.queryCustomerTaskForSystem(paraMap);
			return ResponseResult.ok(res);
		} catch (Exception ex) {
			return ResponseResult.error("加载客户任务出错，请联系管理员!");
		}
	}
}

